---
title: "Python Import"
date: 2018-3-31
categories:
- python
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org673bc26">Example 1, Import a python module with python interpreter:</a></li>
<li><a href="#orgb3d817b">Example 2, Use execfile or (exec in Python 3) in a script to execute the other python file in place:</a></li>
<li><a href="#orgaab1813">Example 3, Use from … import … functionality:</a></li>
<li><a href="#org3b2a199">Example 4, Import riaa.py if it's in a different file location from where it is imported</a></li>
<li><a href="#orgd5192e0">Example 5, use os.system("python yourfile.py")</a></li>
<li><a href="#orgb6a95ff">Example 6, import your file via piggybacking the python startuphook:</a></li>
<li><a href="#org47ad4d9">Example 7, Most Robust: Import files in python with the bare import command:</a></li>
</ul>
</div>
</div>
<blockquote>
<p>
<a href="https://stackoverflow.com/questions/2349991/python-how-to-import-other-python-files">https://stackoverflow.com/questions/2349991/python-how-to-import-other-python-files</a>
</p>
</blockquote>
<p>
There are many ways to import a python file, all with their pros and cons.
</p>
<p>
Don't just hastily pick the first import strategy that works for you or else you'll have to rewrite the codebase later on when you find it doesn't meet your needs.
</p>
<p>
I'll start out explaining the easiest example #1, then I'll move toward the most professional and robust example #7
</p>
<div class="outline-2" id="outline-container-org673bc26">
<h2 id="org673bc26">Example 1, Import a python module with python interpreter:</h2>
<div class="outline-text-2" id="text-org673bc26">
<ol class="org-ol">
<li>Put this in /home/el/foo/fox.py:</li>
</ol>
<div class="org-src-container">
<pre class="src src-python"><span style="font-weight: bold;">def</span> <span style="font-weight: bold;">what_does_the_fox_say</span>():
    <span style="font-weight: bold;">print</span>(<span style="font-style: italic;">"vixens cry"</span>)
</pre>
</div>
<ol class="org-ol">
<li>Get into the python interpreter:</li>
</ol>
<div class="org-src-container">
<pre class="src src-sh">el@apollo:/home/el/foo$ python
  Python 2.7.3 (default, Sep 26 2013, 20:03:06) 
  &gt;&gt;&gt; import fox
  &gt;&gt;&gt; fox.what_does_the_fox_say()
  vixens cry
  &gt;&gt;&gt; 
</pre>
</div>
<p>
You imported fox through the python interpreter, invoked the python function ```what_does_the_fox_say()``` from within fox.py.
</p>
</div>
</div>
<div class="outline-2" id="outline-container-orgb3d817b">
<h2 id="orgb3d817b">Example 2, Use execfile or (exec in Python 3) in a script to execute the other python file in place:</h2>
<div class="outline-text-2" id="text-orgb3d817b">
<ol class="org-ol">
<li>Put this in /home/el/foo2/mylib.py:</li>
</ol>
<div class="org-src-container">
<pre class="src src-python"><span style="font-weight: bold;">def</span> <span style="font-weight: bold;">moobar</span>():
  <span style="font-weight: bold;">print</span>(<span style="font-style: italic;">"hi"</span>)
</pre>
</div>
<ol class="org-ol">
<li>Put this in /home/el/foo2/main.py:</li>
</ol>
<div class="org-src-container">
<pre class="src src-python"><span style="font-weight: bold;">execfile</span>(<span style="font-style: italic;">"/home/el/foo2/mylib.py"</span>)
moobar()
</pre>
</div>
<ol class="org-ol">
<li>run the file:</li>
</ol>
<pre class="example">
el@apollo:/home/el/foo$ python main.py
hi
</pre>
<p>
The function moobar was imported from mylib.py and made available in main.py
</p>
</div>
</div>
<div class="outline-2" id="outline-container-orgaab1813">
<h2 id="orgaab1813">Example 3, Use from … import … functionality:</h2>
<div class="outline-text-2" id="text-orgaab1813">
<ol class="org-ol">
<li>Put this in /home/el/foo3/chekov.py:</li>
</ol>
<div class="org-src-container">
<pre class="src src-python"><span style="font-weight: bold;">def</span> <span style="font-weight: bold;">question</span>():
  <span style="font-weight: bold;">print</span> <span style="font-style: italic;">"where are the nuclear wessels?"</span>
</pre>
</div>
<ol class="org-ol">
<li>Put this in /home/el/foo3/main.py:</li>
</ol>
<div class="org-src-container">
<pre class="src src-python"><span style="font-weight: bold;">from</span> chekov <span style="font-weight: bold;">import</span> question
question()
</pre>
</div>
<ol class="org-ol">
<li>Run it like this:</li>
</ol>
<div class="org-src-container">
<pre class="src src-python">el@apollo:/home/el/foo3$ python main.py 
where are the nuclear wessels?
</pre>
</div>
<p>
If you defined other functions in chekov.py, they would not be available unless you import *
</p>
</div>
</div>
<div class="outline-2" id="outline-container-org3b2a199">
<h2 id="org3b2a199">Example 4, Import riaa.py if it's in a different file location from where it is imported</h2>
<div class="outline-text-2" id="text-org3b2a199">
<ol class="org-ol">
<li>Put this in /home/el/foo4/stuff/riaa.py:</li>
</ol>
<div class="org-src-container">
<pre class="src src-python"><span style="font-weight: bold;">def</span> <span style="font-weight: bold;">watchout</span>():
  <span style="font-weight: bold;">print</span> <span style="font-style: italic;">"computers are transforming into a noose and a yoke for humans"</span>
</pre>
</div>
<ol class="org-ol">
<li>Put this in /home/el/foo4/main.py:</li>
</ol>
<div class="org-src-container">
<pre class="src src-python"><span style="font-weight: bold;">import</span> sys 
<span style="font-weight: bold;">import</span> os
sys.path.append(os.path.abspath(<span style="font-style: italic;">"/home/el/foo4/stuff"</span>))
<span style="font-weight: bold;">from</span> riaa <span style="font-weight: bold;">import</span> *
watchout()
</pre>
</div>
<ol class="org-ol">
<li>Run it:</li>
</ol>
<div class="org-src-container">
<pre class="src src-python">el@apollo:/home/el/foo4$ python main.py 
computers are transforming into a noose <span style="font-weight: bold;">and</span> a yoke <span style="font-weight: bold;">for</span> humans
</pre>
</div>
<p>
That imports everything in the foreign file from a different directory.
</p>
</div>
</div>
<div class="outline-2" id="outline-container-orgd5192e0">
<h2 id="orgd5192e0">Example 5, use os.system("python yourfile.py")</h2>
<div class="outline-text-2" id="text-orgd5192e0">
<div class="org-src-container">
<pre class="src src-python"><span style="font-weight: bold;">import</span> os
os.system(<span style="font-style: italic;">"python yourfile.py"</span>)
</pre>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-orgb6a95ff">
<h2 id="orgb6a95ff">Example 6, import your file via piggybacking the python startuphook:</h2>
<div class="outline-text-2" id="text-orgb6a95ff">
<p>
See: <a href="https://docs.python.org/2/library/user.html">https://docs.python.org/2/library/user.html</a>
</p>
<p>
Put this code into your home directory in ~/.pythonrc.py
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="font-weight: bold;">class</span> <span style="font-weight: bold; text-decoration: underline;">secretclass</span>:
<span style="font-weight: bold;">def</span> <span style="font-weight: bold;">secretmessage</span>(cls, myarg):
<span style="font-weight: bold;">return</span> myarg + <span style="font-style: italic;">" is if.. up in the sky, the sky"</span>
<span style="font-weight: bold; font-style: italic;">secretmessage</span> = <span style="font-weight: bold;">classmethod</span>( secretmessage )

<span style="font-weight: bold;">def</span> <span style="font-weight: bold;">skycake</span>(cls):
<span style="font-weight: bold;">return</span> <span style="font-style: italic;">"cookie and sky pie people can't go up and "</span>
<span style="font-weight: bold; font-style: italic;">skycake</span> = <span style="font-weight: bold;">classmethod</span>( skycake )
<span style="font-weight: bold; font-style: italic;">#</span><span style="font-weight: bold; font-style: italic;">+EDN_SRC</span>
Put this code into your main.py (can be anywhere):
<span style="font-weight: bold; font-style: italic;">#</span><span style="font-weight: bold; font-style: italic;">+BEGIN_SRC python</span>
<span style="font-weight: bold;">import</span> user
<span style="font-weight: bold; font-style: italic;">msg</span> = <span style="font-style: italic;">"The only way skycake tates good"</span> 
<span style="font-weight: bold; font-style: italic;">msg</span> = user.secretclass.secretmessage(msg)
<span style="font-weight: bold; font-style: italic;">msg</span> += user.secretclass.skycake()
<span style="font-weight: bold;">print</span>(msg + <span style="font-style: italic;">" have the sky pie! SKYCAKE!"</span>)
</pre>
</div>
<p>
Run it:
</p>
<div class="org-src-container">
<pre class="src src-python">$ python main.py
The only way skycake tates good <span style="font-weight: bold;">is</span> <span style="font-weight: bold;">if</span>.. up <span style="font-weight: bold;">in</span> the sky, 
the skycookie <span style="font-weight: bold;">and</span> sky pie people can<span style="font-style: italic;">'t go up and  have the sky pie! </span>
<span style="font-style: italic;">SKYCAKE!</span>
</pre>
</div>
<p>
Credit for this jist goes to: <a href="https://github.com/docwhat/homedir-examples/blob/master/python-commandline/.pythonrc.py">https://github.com/docwhat/homedir-examples/blob/master/python-commandline/.pythonrc.py</a> Send along your up-boats.
</p>
</div>
</div>
<div class="outline-2" id="outline-container-org47ad4d9">
<h2 id="org47ad4d9">Example 7, Most Robust: Import files in python with the bare import command:</h2>
<div class="outline-text-2" id="text-org47ad4d9">
<ol class="org-ol">
<li>Make a new directory <i>home/el/foo5</i></li>
<li>Make a new directory /home/el/foo5/herp</li>
<li>Make an empty file named <span class="underline"><span class="underline">init</span></span>.py under herp:</li>
</ol>
<div class="org-src-container">
<pre class="src src-sh">el@apollo:/home/el/foo5/herp$ touch __init__.py
el@apollo:/home/el/foo5/herp$ ls
__init__.py
</pre>
</div>
<ol class="org-ol">
<li>Make a new directory /home/el/foo5/herp/derp</li>
<li>Under derp, make another <span class="underline"><span class="underline">init</span></span>.py file:</li>
</ol>
<div class="org-src-container">
<pre class="src src-sh">el@apollo:/home/el/foo5/herp/derp$ touch __init__.py
el@apollo:/home/el/foo5/herp/derp$ ls
__init__.py
</pre>
</div>
<ol class="org-ol">
<li>Under /home/el/foo5/herp/derp make a new file called yolo.py Put this in there:</li>
</ol>
<div class="org-src-container">
<pre class="src src-python"><span style="font-weight: bold;">def</span> <span style="font-weight: bold;">skycake</span>():
  <span style="font-weight: bold;">print</span> <span style="font-style: italic;">"SkyCake evolves to stay just beyond the cognitive reach of "</span> +
  <span style="font-style: italic;">"the bulk of men. SKYCAKE!!"</span>
</pre>
</div>
<ol class="org-ol">
<li>The moment of truth, Make the new file /home/el/foo5/main.py, put this in there;</li>
</ol>
<div class="org-src-container">
<pre class="src src-python"><span style="font-weight: bold;">from</span> herp.derp.yolo <span style="font-weight: bold;">import</span> skycake
skycake()
</pre>
</div>
<ol class="org-ol">
<li>Run it:</li>
</ol>
<div class="org-src-container">
<pre class="src src-sh">el@apollo:/home/el/foo5$ python main.py
SkyCake evolves to stay just beyond the cognitive reach of the bulk 
of men. SKYCAKE!!
</pre>
</div>
<p>
The empty <span class="underline"><span class="underline">init</span></span>.py file communicates to the python interpreter that the developer intends this directory to be an importable package.
</p>
<p>
If you want to see my post on how to include ALL .py files under a directory see here: <a href="https://stackoverflow.com/a/20753073/445131">https://stackoverflow.com/a/20753073/445131</a>
</p>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2018-3-31</p>
<p class="author">Author: stackoverflow</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
